﻿using Core.Context;
using Core.Dtos;
using Core.Domain;
using Core.Factories;
using Core.IServices;
using Core.Messages;
using Core.Queries;
using Library.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Front.Areas.Admin.Controllers.Api
{
    [RoutePrefix("api/admin/users/{userId}/roles")]
    //[Authorize(Roles = "Admin")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class UserRoleApiController : ApiController
    {
        private readonly IManageUserService userService;

        public UserRoleApiController(IManageUserService userService)
        {
            this.userService = userService;
        }

        [Route("searches")]
        [HttpPost]
        public QueryResponse<RoleDto> FindBy(string userId)
        {
            return userService.GetRoles(userId);
        }

        [HttpPost]
        [Route("")]
        public Response Post(string userId, RoleDto role)
        {
            return userService.AddToRole(userId, role.Name);
        }

        [HttpDelete]
        [Route("{role}")]
        public Response Delete(string userId, string role)
        {
            return userService.RemoveFromRole(userId, role);
        }
    }
}
